//
//  DBYRoadDetailViewController.m
//  MemoryTravel
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 fcx. All rights reserved.
//

#import "DBYRoadDetailViewController.h"

@interface DBYRoadDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) UIView *titleView;
@property (nonatomic, retain) NSMutableArray *memoArray;
@property (nonatomic, retain) NSMutableArray *planArray;
@property (nonatomic, retain) NSMutableArray *cellArray;
@end

@implementation DBYRoadDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.memoArray = [NSMutableArray array];
    self.planArray = [NSMutableArray array];
    self.cellArray = [NSMutableArray array];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Width, Height - 66) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    

    
    UIImageView *titleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Width, 220)];
    [titleImageView sd_setImageWithURL:[NSURL URLWithString:self.aModel.image_url] placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
    self.myTableView.tableHeaderView = titleImageView;
//    [titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.top.and.left.mas_equalTo(0);
//        make.size.mas_equalTo(CGSizeMake(Width, 220));
//        
//    }];


    UILabel *titleLabel = [[UILabel alloc]init];
    [titleImageView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(160);
        make.left.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(Width - 20, 15));
    }];
    titleLabel.text = self.aModel.name;
    titleLabel.font = [UIFont boldSystemFontOfSize:15];
    titleLabel.textColor = [UIColor whiteColor];
    
    NSString *count = [NSString stringWithFormat:@"%@天 / %@个旅行地", self.aModel.plan_days_count, self.aModel.plan_nodes_count];
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.text = count;
    timeLabel.font = [UIFont boldSystemFontOfSize:15];
    timeLabel.textColor = [UIColor whiteColor];
    [titleImageView addSubview:timeLabel];

    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(180);
        make.left.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(Width - 20, 15));
    }];
    [self.myTableView registerClass:[DBYRoadDetailTableViewCell class] forCellReuseIdentifier:@"DBYRoadDetailTableViewCell"];
    [self.myTableView registerClass:[DBYRoadDetailTodayTableViewCell class] forCellReuseIdentifier:@"DBYRoadDetailTodayTableViewCell"];

    [self getAFNetWorkingData];
    
    // Do any additional setup after loading the view.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.memoArray.count;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return [[self.cellArray objectAtIndex:section] integerValue];
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSInteger sum = 0;
    for (int i = 0; i < indexPath.section; i++) {
        sum +=  [[self.cellArray objectAtIndex:i] integerValue];
    }
    
    if (indexPath.row == 0) {
    
        DBYRoadDetailTodayTableViewCell *roadDetailTodayCell = [tableView dequeueReusableCellWithIdentifier:@"DBYRoadDetailTodayTableViewCell"];
        
        roadDetailTodayCell.memo = [self.memoArray objectAtIndex:indexPath.section];
            return roadDetailTodayCell;
    }else{
        DBYRoadDetailTableViewCell *roadDetailCell = [tableView dequeueReusableCellWithIdentifier:@"DBYRoadDetailTableViewCell"];
        roadDetailCell.selectionStyle = UITableViewCellSelectionStyleNone;

        roadDetailCell.roadDetailModel = [self.planArray objectAtIndex:(indexPath.row + sum)];
        roadDetailCell.s = [NSString stringWithFormat:@"%ld",indexPath.row];
        return roadDetailCell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    DBYRoadDetailModel *roadDetailModel = [[DBYRoadDetailModel alloc] init];
    NSInteger sum = 0;
    for (int i = 0; i < indexPath.section; i++) {
        sum +=  [[self.cellArray objectAtIndex:i] integerValue];
    }
    roadDetailModel = [self.planArray objectAtIndex:sum + indexPath.row];
    CGFloat height;
    
    if (indexPath.row == 0) {
        height = [DBYRoadDetailTableViewCell heightForContent:[self.memoArray objectAtIndex:indexPath.section]];
        return height + 70;
    
    }else{
        height = [DBYRoadDetailTableViewCell heightForContent:roadDetailModel.tips];
        return height + 300;
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSInteger sum = 0;
    for (int i = 0; i < section; i++) {
        sum +=  [[self.cellArray objectAtIndex:i] integerValue];
    }
    
    DBYRoadDetailModel *roadDetailModel = [[DBYRoadDetailModel alloc] init];
    if (self.planArray.count > sum +2 || self.planArray.count == 2) {
        roadDetailModel = [self.planArray objectAtIndex:sum + 2];
    }else if(self.planArray.count == 1){
        roadDetailModel = [self.planArray objectAtIndex:sum + 1];
    }else{
        roadDetailModel = [self.planArray objectAtIndex:sum];
    }
    
    NSString *place = [roadDetailModel.destination objectForKey:@"name_zh_cn"];
    NSString *data = [NSString stringWithFormat:@"DAY%ld %@", section + 1, place];
    
    return data;
}


-(void)getAFNetWorkingData
{
    NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/plans/%@.json?page=1",self.aModel.nameId];
    
    
    
    [AppTools getDataWithUrlStr:url andBlock:^(id result) {
        
        NSMutableDictionary *bigDic = result;
        NSMutableArray *array = [bigDic objectForKey:@"plan_days"];
        
        for (NSMutableDictionary *dic in array) {
            NSString *memo = [dic objectForKey:@"memo"];
            [self.memoArray addObject:memo];
            NSMutableArray *mArray = [dic objectForKey:@"plan_nodes"];
            NSInteger count = mArray.count;
            NSString *str = [NSString stringWithFormat:@"%ld",count];
            [self.cellArray addObject:str];
            
            for (NSMutableDictionary *mDic in mArray) {
                DBYRoadDetailModel *roadDetailModel = [[DBYRoadDetailModel alloc] init];
                [roadDetailModel setValuesForKeysWithDictionary:mDic];
                [self.planArray addObject:roadDetailModel];
            }
            
        }
        [self.myTableView reloadData];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
